"""
基于Milvus的对话系统
"""

import json

from flask import Flask, request
from flask import render_template

from service.milvus_service import milvus_answer_search, milvus_upload_data

app = Flask(__name__)

milvus_upload_data()


@app.route('/', methods=['GET', 'POST'])
def index():
    return render_template('index.html')


@app.route('/webhook/qa', methods=['POST'])
def qa():
    text = [k for k, v in request.form.to_dict().items()][0]
    print(text)
    question = json.loads(text)['message']
    print("问题:", question)
    answer = milvus_answer_search(question)
    print(answer)
    return json.dumps([{'text': answer}])


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=18234)
